package com.kedacom.freedomLabel.view.widget.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.model.bean.IFirstLetterComparable;
import com.kedacom.lego.fast.widget.refreshlayout.util.DensityUtil;

import java.util.List;

public class IDStickyItemDecoration<T extends IFirstLetterComparable> extends RecyclerView.ItemDecoration {

    private int headerHeight;
    private Paint mPaint;
    private Paint mTextPaint;
    private List<T> list;

    public IDStickyItemDecoration(Context context, List<T> list) {
        this.list = list;
        headerHeight = DensityUtil.dp2px(28);
        mPaint = new Paint();
        mPaint.setColor(context.getResources().getColor(R.color.gray_bg));
        mTextPaint = new Paint();
        mTextPaint.setStrokeWidth(DensityUtil.dp2px(1));
        mTextPaint.setTextSize(DensityUtil.dp2px(14));
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(context.getResources().getColor(R.color.gray_333));
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        if (position == -1 || list == null || list.size() == 0) return;
        if (position == 0 || !list.get(position).getFirstLetter().equals(list.get(position - 1).getFirstLetter())) {
            outRect.set(0, headerHeight, 0, 0);
        }
    }

    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        if (list == null || list.size() == 0) return;
        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int firstItemPosition = parent.getChildLayoutPosition(parent.getChildAt(0));
        if (firstItemPosition >= 0) {
            drawHeaderRect(c, list.get(firstItemPosition).getFirstLetter(), left, 0, right, headerHeight);
        }
        for (int i = 0; i < childCount; i++) {
            if (isItemView(parent, i)) {
                View itemView = parent.getChildAt(i);
                int top = itemView.getTop() - headerHeight;
                int bottom = itemView.getTop();
                int position = parent.getChildAdapterPosition(itemView);
                if (position != -1 && (position == 0 || !list.get(position).getFirstLetter().equals(list.get(position - 1).getFirstLetter()))) {
                    if (top < 0) {
                        if (top > -itemView.getHeight()) {
                            top = 0;
                            bottom = headerHeight;
                        }
                    }
                    drawHeaderRect(c, list.get(position).getFirstLetter(), left, top, right, bottom);
                }
            }
        }
    }

    private void drawHeaderRect(Canvas c, String firstLetter, int left, int top, int right, int bottom) {
        c.drawRect(left, top, right, bottom, mPaint);
        float titleX = DensityUtil.dp2px(15);
        float baseLine = (float) (top + bottom) / 2 + DensityUtil.dp2px(5);
        c.drawText(firstLetter, titleX, baseLine, mTextPaint);
    }

    private boolean isItemView(RecyclerView parent, int position) {
        return parent.getAdapter().getItemViewType(position) >= 0;
    }
}